﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using HRM.HRMServiceProxy;


namespace HRM
{
    public partial class frmLaterInsuranceInfo : Form
    {
        bool isChange = false;
        DepartmentClient wcfDepartment;
        EmployeeClient wcfEmployee;
        EmployeeInfo _EmployeeInfo;
        PositionDetailClient wcfPD;
        MarkWorkClient wcfMarkWork;
        LeaveWorkClient wcfFM;
        ParameterClient wcfParameter;
        int _LaterInsuranceID;
        decimal _SumLaterInsuranceArisen, _SumLaterInsuranceDeduction, _SumLaterInsuranceEarn;
        int _LaterInsurance = 620000;
        int _datework = 26;
        LaterInsuranceClient wcfLaterInsurance;
        LaterInsuranceInfo _LaterInsuranceInfo;
        DialogResult dlgResult;
        int _month = DateTime.Now.Month;
        int _year = DateTime.Now.Year;
        //bool _SocialEmployee;
        //bool _MedicalEmployee;
        //int _EmployeeID;
        //int _CloseBookDay;
        RoleRightInfo listRole;

        public frmLaterInsuranceInfo(int LaterInsuranceID, RoleRightInfo _listRole)
        {
            _LaterInsuranceID = LaterInsuranceID;
            InitializeComponent();
            Language();
            LaterInsuranceClient wcfLaterInsurance = Service.LaterInsurance();
            dlgResult = DialogResult.Cancel;
            _LaterInsuranceInfo = new LaterInsuranceInfo();
            cboMonth.Text = DateTime.Now.Month.ToString();
            cboYear.Text = DateTime.Now.Year.ToString();
            if (_LaterInsuranceID > 0)
            {
                _LaterInsuranceInfo = wcfLaterInsurance.LaterInsurance_GetInfo(_LaterInsuranceID);
                Info2Form();
            }
            listRole = _listRole;
        }

        private void Language()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString("thongtinluongcanbo");
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
            UpdateUI.Load_GroupBox(ref groupBox1);
            UpdateUI.Load_GroupBox(ref groupBox2);
            UpdateUI.Load_GroupBox(ref groupBox3);
            UpdateUI.Load_GroupBox(ref groupBox4);
        }

        private void Info2Form()//Hiển thị thông tin ra form
        {
            wcfPD = Service.PositionDetail();
            _EmployeeInfo = new EmployeeInfo();
            _EmployeeInfo = wcfEmployee.Employee_GetInfo(_LaterInsuranceInfo.EmployeeID);
            PositionDetailInfo _PDInfo = new PositionDetailInfo();
            _PDInfo = wcfPD.PositionDetail_GetInfo(_LaterInsuranceInfo.EmployeeID);
            txtDepartment.Text = wcfDepartment.Department_GetInfo(_PDInfo.DepartmentID).Name;
            txtFullName.Text = _EmployeeInfo.LastName + " " + _EmployeeInfo.FirstName;
            txtFullName.Tag = _LaterInsuranceInfo.EmployeeID;
            DataTable dt = wcfPD.PositionDetail_GetWithEmployee(_LaterInsuranceInfo.EmployeeID).Tables[0];
            txtPosition.Text = dt.Rows[0]["PositionName"].ToString();

            txtProbation.Text = _LaterInsuranceInfo.ProbationFactor.ToString(UIParamater.Cham2So);
            txtDateWork.Text = _LaterInsuranceInfo.TotalDateWork.ToString();
            txtDateOutWork.Text = _LaterInsuranceInfo.TotalDateOutWork.ToString();
            txtOverTime.Text = _LaterInsuranceInfo.OverTime.ToString();
            cboMonth.Text = _LaterInsuranceInfo.Date.Month.ToString();
            cboYear.Text = _LaterInsuranceInfo.Date.Year.ToString();
            txtFurloughPaid.Text = _LaterInsuranceInfo.TotalLeavePaid.ToString();
            txtFurloughUnPaid.Text = _LaterInsuranceInfo.TotalLeaveUnPaid.ToString();
            txtBasicFactory.Text = _LaterInsuranceInfo.BasicFactor.ToString(UIParamater.Cham2So);
            txtBasicSalary.Text = _LaterInsuranceInfo.BasicSalary.ToString(UIParamater.Phay2So);
            txtComFactory.Text = _LaterInsuranceInfo.CompanyFactor.ToString(UIParamater.Cham2So);
            txtComSalary.Text = _LaterInsuranceInfo.CompanySalary.ToString(UIParamater.Phay2So);
            txtComLunch.Text = _LaterInsuranceInfo.CompanyLunch.ToString(UIParamater.Phay2So);
            txtTelephoneCost.Text = _LaterInsuranceInfo.TelephoneCost.ToString(UIParamater.Phay2So);
            txtPositionFactor.Text = _LaterInsuranceInfo.PositionFactor.ToString(UIParamater.Cham2So);
            txtOverSeniorityFactor.Text = _LaterInsuranceInfo.OverSeniorityFactor.ToString(UIParamater.Cham2So);
            txtSeniorityFactor.Text = _LaterInsuranceInfo.SeniorityFactor.ToString(UIParamater.Cham2So);
            txtResponseFactory.Text = _LaterInsuranceInfo.ResponseFactor.ToString(UIParamater.Cham2So);
            txtResponseSalary.Text = _LaterInsuranceInfo.ResponseSalary.ToString(UIParamater.Phay2So);
            txtTradeUnion.Text = _LaterInsuranceInfo.TradeUnion.ToString(UIParamater.Phay2So);
            txtIncomeTax.Text = _LaterInsuranceInfo.IncomeTax.ToString(UIParamater.Phay2So);
            if (_LaterInsuranceInfo.EmployeeSocialInc < 0)
                txtSocialInc.Text = "";
            else
                txtSocialInc.Text = _LaterInsuranceInfo.EmployeeSocialInc.ToString(UIParamater.Phay2So);
            if (_LaterInsuranceInfo.EmployeeMedicalInc < 0)
                txtMedicalInc.Text = "";
            else
                txtMedicalInc.Text = _LaterInsuranceInfo.EmployeeMedicalInc.ToString(UIParamater.Phay2So);
            txtOtherResponseSalary.Text = _LaterInsuranceInfo.OtherResponse.ToString(UIParamater.Phay2So);
            txtOtherDeduction.Text = _LaterInsuranceInfo.OtherDeduction.ToString(UIParamater.Phay2So);
            txtSumSalary_arisen.Text = (_LaterInsuranceInfo.BasicSalary + _LaterInsuranceInfo.CompanySalary + _LaterInsuranceInfo.ResponseSalary + _LaterInsuranceInfo.OtherResponse).ToString(UIParamater.Phay2So);
            txtSumSalary_Deduction.Text = (_LaterInsuranceInfo.IncomeTax + _LaterInsuranceInfo.OtherDeduction + _LaterInsuranceInfo.TelephoneCost + _LaterInsuranceInfo.TradeUnion + _LaterInsuranceInfo.CompanyLunch + _LaterInsuranceInfo.EmployeeMedicalInc + _LaterInsuranceInfo.EmployeeSocialInc).ToString(UIParamater.Phay2So);
            txtSumSalary.Text = (Convert.ToDecimal(txtSumSalary_arisen.Text) - Convert.ToDecimal(txtSumSalary_Deduction.Text)).ToString(UIParamater.Phay2So);

            isChange = false;
        }

        private void Form2Info()//Chuyển dữ liệu từ form nhập vào Info
        {
            _LaterInsuranceInfo.ID = _LaterInsuranceID;
            _LaterInsuranceInfo.EmployeeID = Convert.ToInt32(txtFullName.Tag);
            wcfPD = Service.PositionDetail();
            DataTable dt = wcfPD.PositionDetail_GetWithEmployee(_LaterInsuranceInfo.EmployeeID).Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                _LaterInsuranceInfo.PositionID = Convert.ToInt32(dr["PD_PositionID"].ToString());
            }
            _LaterInsuranceInfo.OverTime = Convert.ToDecimal(txtOverTime.Text);
            if (Convert.ToInt32(cboMonth.Text) < 10)
                _LaterInsuranceInfo.Date = DateTime.ParseExact("0" + cboMonth.Text + "/" + cboYear.Text, "MM/yyyy", null);
            else
                _LaterInsuranceInfo.Date = DateTime.ParseExact(cboMonth.Text + "/" + cboYear.Text, "MM/yyyy", null);

            _LaterInsuranceInfo.BasicFactor = Convert.ToDecimal(txtBasicFactory.Text);
            _LaterInsuranceInfo.BasicSalary = Convert.ToDecimal(txtBasicSalary.Text);
            _LaterInsuranceInfo.CompanyFactor = Convert.ToDecimal(txtComFactory.Text);
            _LaterInsuranceInfo.CompanySalary = Convert.ToDecimal(txtComSalary.Text);
            _LaterInsuranceInfo.ResponseFactor = Convert.ToDecimal(txtResponseFactory.Text);
            _LaterInsuranceInfo.ResponseSalary = Convert.ToDecimal(txtResponseSalary.Text);
            _LaterInsuranceInfo.IncomeTax = Convert.ToDecimal(txtIncomeTax.Text);
            _LaterInsuranceInfo.TradeUnion = Convert.ToDecimal(txtTradeUnion.Text);
            _LaterInsuranceInfo.CompanyLunch = Convert.ToDecimal(txtComLunch.Text);
            _LaterInsuranceInfo.TelephoneCost = Convert.ToDecimal(txtTelephoneCost.Text);
            _LaterInsuranceInfo.OtherResponse = Convert.ToDecimal(txtOtherResponseSalary.Text);
            _LaterInsuranceInfo.OtherDeduction = Convert.ToDecimal(txtOtherDeduction.Text);
            _LaterInsuranceInfo.ProbationFactor = Convert.ToDecimal(txtProbation.Text);
            _LaterInsuranceInfo.TotalDateWork = Convert.ToInt32(txtDateWork.Text);
            _LaterInsuranceInfo.TotalDateOutWork = Convert.ToInt32(txtDateOutWork.Text);
            _LaterInsuranceInfo.TotalLeavePaid = Convert.ToInt32(txtFurloughPaid.Text);
            _LaterInsuranceInfo.TotalLeaveUnPaid = Convert.ToInt32(txtFurloughUnPaid.Text);
            //_LaterInsuranceInfo.EmployeeSocialInc=

        }

        bool checkValid() //Kiểm tra dữ liệu nhập
        {
            decimal sal;
            if (txtOverTime.Text.Trim().Length == 0)
            {
                txtOverTime.Focus();
                MessageBox.Show(UpdateUI.GetString("thoigianlamthemkhongduocdetrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (txtProbation.Text.Trim().Length == 0)
            {
                txtProbation.Focus();
                MessageBox.Show(UpdateUI.GetString("tileluongkhongduocdetrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else
                if (!decimal.TryParse(txtProbation.Text, out sal))
                {
                    txtProbation.Text = "";
                    txtProbation.Focus();
                    MessageBox.Show(UpdateUI.GetString("tileluongkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            return true;
        }

        private void frmLaterInsuranceInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //Tổng lương tăng trong tháng:
        private void txtOtherResponseLaterInsurance_Leave(object sender, EventArgs e)
        {
            _SumLaterInsuranceArisen = (((Convert.ToDecimal(txtBasicSalary.Text)
                + Convert.ToDecimal(txtComSalary.Text) + Convert.ToDecimal(txtResponseSalary.Text)
                + Convert.ToDecimal(txtOtherResponseSalary.Text)) * (Convert.ToDecimal(txtProbation.Text))
                / _datework) * (Convert.ToDecimal(txtDateWork.Text)));
            txtSumSalary_arisen.Text = _SumLaterInsuranceArisen.ToString(UIParamater.Phay2So);
        }

        //Tổng lương giảm trong tháng:
        private void txtOtherDeduction_Leave(object sender, EventArgs e)
        {
            _SumLaterInsuranceDeduction = (Convert.ToDecimal(txtIncomeTax.Text)
                + Convert.ToDecimal(txtTradeUnion.Text) + Convert.ToDecimal(txtComLunch.Text)
                + Convert.ToDecimal(txtTelephoneCost.Text) + Convert.ToDecimal(txtOtherDeduction.Text));
            txtSumSalary_Deduction.Text = _SumLaterInsuranceDeduction.ToString(UIParamater.Phay2So);
        }

        //Tổng lương được lĩnh trong tháng:
        private void txtSumLaterInsurance_Deduction_Leave(object sender, EventArgs e)
        {
            _SumLaterInsuranceEarn = (_SumLaterInsuranceArisen - _SumLaterInsuranceDeduction);
            txtSumSalary.Text = _SumLaterInsuranceEarn.ToString(UIParamater.Phay2So);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!checkValid())
            {
                isChange = true;
                return;

            }
            LaterInsuranceClient wcfLaterInsurance = Service.LaterInsurance();
            Form2Info();
            if (_LaterInsuranceID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (wcfLaterInsurance.LaterInsurance_Update(_LaterInsuranceInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _LaterInsuranceInfo.ID = wcfLaterInsurance.LaterInsurance_Insert(_LaterInsuranceInfo);
                if (_LaterInsuranceInfo.ID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
        }

        private void frmLaterInsuranceInfo_Activated(object sender, EventArgs e)
        {
            Language();
        }

        private void frmLaterInsuranceInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            wcfDepartment.Close();
            wcfEmployee.Close();
            wcfFM.Close();
            wcfLaterInsurance.Close();
            wcfMarkWork.Close();
            wcfParameter.Close();
            wcfPD.Close();
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
